﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.Common;
using System.Collections;
using System.Web;

namespace SemiCASE.DataType.Common.Logging
{
    
   public class LogWriter:ILogger
    {

       EventLogTraceListener  eventLogListener;
      
       public LogWriter()
       {
          eventLogListener=new EventLogTraceListener ("Application");
           
           eventLogListener.TraceOutputOptions = TraceOptions.Callstack | TraceOptions.DateTime | TraceOptions.LogicalOperationStack | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Timestamp;
           

       }

       public void Write(GumrukModules moduleId, ModuleOperations operationId, EventLogEntryType eventType, Exception ex, string sayfaUrl, string message, string kullaniciSicil, string pcName, Dictionary<string, object> extendedParameters)
       {
           StringBuilder sb = new StringBuilder();
           
           sb.Append("PC Adı :");
           sb.Append(pcName);
           sb.Append(Environment.NewLine);

           sb.Append("Kullanıcı Sicil :");
           sb.Append(kullaniciSicil);
           sb.Append(Environment.NewLine);

           sb.Append("Mesaj :");
           sb.Append(message);
           sb.Append(Environment.NewLine);

           sb.Append("Sayfa URL :");
           sb.Append(sayfaUrl);
           sb.Append(Environment.NewLine);

           if (ex != null)
           {
               sb.Append("Metod Adı : ");
               sb.Append( ex.TargetSite.ToString());
               sb.Append(Environment.NewLine);

               sb.Append("Modül Adı : ");
               sb.Append(ex.TargetSite.Module);
               sb.Append(Environment.NewLine);

               if (ex.InnerException != null)
               {
                   sb.Append("Inner Exception : ");
                   sb.Append(ex.InnerException.StackTrace);
                   sb.Append(Environment.NewLine);

               }

               if (ex.Data.Count > 0)
               {
                   sb.Append("parameters :");
                   sb.Append(Environment.NewLine);

                   foreach (DictionaryEntry item in ex.Data)
                   {
                       sb.Append(item.Key);
                       sb.Append(" : ");
                       sb.Append(item.Value);
                       sb.Append(Environment.NewLine);
                   }

               }

           }

           if (extendedParameters != null)
           {
               foreach (KeyValuePair<string,object> item in extendedParameters)
               {
                   sb.Append(item.Key);
                   sb.Append(" : ");
                   sb.Append(item.Value);
                   sb.Append(Environment.NewLine);
               }
           }

           eventLogListener.EventLog.Source = Enum.GetName(typeof(GumrukModules), moduleId);
          
           eventLogListener.EventLog.WriteEntry(sb.ToString(), eventType, 20000);
           
           eventLogListener.Flush();
           eventLogListener.Close();

       }

       public void Write(GumrukModules moduleId, ModuleOperations operationId, EventLogEntryType eventType, string sayfaUrl, string message, Dictionary<string, object> extendedParameters)
       {
           Write(moduleId, operationId, eventType, null, sayfaUrl, message, "", "", extendedParameters);
       }



       public void Write(GumrukModules moduleId, ModuleOperations operationId, EventLogEntryType eventType, string sayfaUrl, string message)
       {
           Write(moduleId, operationId, eventType, null, sayfaUrl, message, "", "", null);
       }

       public void Write(GumrukModules moduleId, EventLogEntryType eventType, Exception ex, string sayfaUrl, string kullaniciSicil, string pcName, string message, params string[] extendedProperties)
       {
           StringBuilder sb = new StringBuilder();

           sb.Append("PC Adı :");
           sb.Append(pcName);
           sb.Append(Environment.NewLine);

           sb.Append("Kullanıcı Sicil :");
           sb.Append(kullaniciSicil);
           sb.Append(Environment.NewLine);

           sb.Append("Mesaj :");
           sb.Append(message);
           sb.Append(Environment.NewLine);

           sb.Append("Sayfa URL :");
           sb.Append(sayfaUrl);
           sb.Append(Environment.NewLine);


           if (ex.InnerException != null)
           {
               sb.Append("Inner Exception : ");
               sb.Append(ex.InnerException.StackTrace);
               sb.Append(Environment.NewLine);
           }

           if (ex != null)
           {
               sb.Append("Metod Adı : ");
               sb.Append(ex.TargetSite.ToString());
               sb.Append(Environment.NewLine);

               sb.Append("Modül Adı : ");
               sb.Append(ex.TargetSite.Module);
               sb.Append(Environment.NewLine);

               if (ex.Data.Count > 0)
               {

                   foreach (DictionaryEntry item in ex.Data)
                   {
                       sb.Append(item.Key);
                       sb.Append(" : ");
                       sb.Append(item.Value);
                       sb.Append(Environment.NewLine);
                   }

               }

           }

           if (extendedProperties != null)
           {
               foreach (var item in extendedProperties)
               {
                   sb.Append(item);
                  
                   sb.Append(Environment.NewLine);
               }
           }

           eventLogListener.EventLog.Source = Enum.GetName(typeof(GumrukModules), moduleId);

           eventLogListener.EventLog.WriteEntry(sb.ToString(), eventType, 20000);
           eventLogListener.Flush();
           eventLogListener.Close();
       }

       public void WriteAudit(string tableName, string rowID, string columnName, TableOperations operation, string user, string projectName, string oldValue, string newValue, string message)
       {


       }

    }
}
